<template>
  <div class="mod-admin-groupActivity">
    <avue-crud
      ref="crud"
      :page="page"
      :data="dataList"
      :table-loading="dataListLoading"
      :option="tableOption"
      @search-change="searchChange"
      @on-load="getDataList"
      @refresh-change="refreshChange"
    >
      <!--<template slot-scope="scope" slot="menuLeft">-->
        <!--<el-button-->
          <!--icon="el-icon-plus"-->
          <!--type="primary"-->
          <!--size="small"-->
          <!--@click="addOrUpdateHandle()"-->
        <!--&gt;{{$t('groups.newJoinGroupActivity')}}</el-button>-->
      <!--</template>-->
      <template slot-scope="scope" slot="menu">
        <el-button
          v-if="scope.row.status === 2"
          style="color: #67C23A"
          icon="el-icon-video-play"
          type="text"
          size="mini"
          @click="activeGroupActivity(scope.row.groupActivityId)"
        >{{$t('groups.startUsing')}}</el-button>
        <el-button
          icon="el-icon-edit"
          size="small"
          type="text"
          @click="addOrUpdateHandle(scope.row.groupActivityId)"
          v-if="isAuth('group:activity:update')"
        >{{$t('groups.editEvent')}}</el-button>
        <el-button
          icon="el-icon-s-opportunity"
          size="small"
          type="text"
          v-if="isAuth('group:activity:manage')"
          @click="manageGroupProdHandle(scope.row.groupActivityId)"
        >{{$t('groups.manageEventProducts')}}</el-button>
        <el-button
          icon="el-icon-folder-delete"
          size="small"
          type="text"
          v-if="isAuth('group:activity:invalid')"
          @click="invalidActivityHandle(scope.row)"
        >
          {{$t('groups.invalidActivity')}}
          <el-tooltip
            class="item"
            effect="dark"
            :content="$t('groups.invalidate')"
            placement="right"
          >
            <i class="el-icon-question"></i>
          </el-tooltip>
        </el-button>
        <el-button
          type="text"
          icon="el-icon-document-checked"
          size="small"
          v-if="isAuth('group:activity:auditApply') && scope.row.status > 2 && scope.row.status < 5"
          @click="auditEventHandle(scope.row.groupActivityId)"
        >
          {{scope.row.status === 3?$t('groups.applyForListing'):$t('coupon.waitReview')}}
          <el-tooltip
            class="item"
            effect="dark"
            :content="$t('groups.ifNotDealtWith')"
            placement="right"
          >
            <i class="el-icon-question"></i>
          </el-tooltip>
        </el-button>
        <!-- 如果活动状态不在 【未开始】、【已结束】、【已失效】的状态下无法进行删除操作-->
        <el-button
          type="text"
          :disabled="!(scope.row.activityStatus == 1 || scope.row.activityStatus == 3 || scope.row.activityStatus == 4)"
          icon="el-icon-delete"
          size="small"
          style="color:red"
          v-if="isAuth('group:activity:delete')"
          @click="deleteHandle(scope.row)"
        >{{$t('text.delBtn')}}</el-button>
      </template>
      <template slot-scope="scope" slot="menuLeft">
        <el-button
          icon="el-icon-plus"
          type="primary"
          size="small"
          v-if="isAuth('group:activity:save')"
          @click="addOrUpdateHandle()"
        >{{$t('groups.newJoinGroupActivity')}}</el-button>
      </template>
      <template slot-scope="scope" slot="groupNumber">
        <el-tag size="mini">{{scope.row.groupNumber}}{{$t('groups.groupOfPeople')}}</el-tag>
      </template>
      <template slot-scope="scope" slot="status">
        <el-tag
          type="info"
          v-if="scope.row.status === 0"
          size="mini"
        >{{$t('groups.expired')}}</el-tag>
        <el-tag
          v-if="scope.row.status === 1"
          size="mini"
        >{{$t('groups.startUsing')}}</el-tag>
        <el-tag
          type="success"
          v-if="scope.row.status === 2"
          size="mini"
        >{{$t('groups.notEnabled')}}</el-tag>
        <el-tag
          type="warning"
          v-if="scope.row.status === 3"
          size="mini"
        >{{$t('groups.offlineViolation')}}</el-tag>
        <el-tag
          type="danger"
          v-if="scope.row.status === 4"
          size="mini"
        >{{$t('groups.moderated')}}</el-tag>
        <el-tag
          type="danger"
          v-if="scope.row.status === 5"
          size="mini"
        >{{$t('groups.over')}}</el-tag>
      </template>
    </avue-crud>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="refreshChange"></add-or-update>
    <!-- 下线管理弹窗-->
    <offlineEventHandle
      v-if="offlineEventHandleVisible"
      selectUrl="/group/activity/getOfflineHandleEventByActivityId"
      applyUrl="/group/activity/auditApply"
      ref="offlineEvent"
      @refreshDataList="refreshChange"
    ></offlineEventHandle>
  </div>
</template>

<script>
import { tableOption } from '@/crud/group/groupActivity'
import AddOrUpdate from './groupActivity-add-or-update'
import OfflineEventHandle from '@/components/offline-event-handle'

export default {
  data () {
    return {
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      searchForm: {},
      dataListLoading: false,
      offlineEventHandleVisible: false,
      tableOption: tableOption,
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate,
    OfflineEventHandle
  },
  created () {
  },
  mounted () {
  },
  methods: {
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/group/activity/page'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign(
            {
              current: page == null ? this.page.currentPage : page.currentPage,
              size: page == null ? this.page.pageSize : page.pageSize
            },
            this.searchForm
          )
        )
      }).then(({ data }) => {
        this.dataList = data.records
        this.page.total = data.total
        this.dataListLoading = false
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    deleteHandle (row) {
      this.$confirm(this.$i18n.t('groups.confirmDelete') + row.activityName + this.$i18n.t('groups.active'), this.$i18n.t('text.tips'), {
        confirmButtonText: this.$i18n.t('crud.filter.submitBtn'),
        cancelButtonText: this.$i18n.t('crud.filter.cancelBtn'),
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/group/activity/' + row.groupActivityId),
          method: 'delete',
          data: this.$http.adornData({})
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('publics.operation'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.refreshChange()
            }
          })
        })
      }).catch(() => {
      })
    },
    /**
     * 刷新回调
     */
    refreshChange (isRouteToGroupProd, groupActivityId) {
      if (isRouteToGroupProd === 1) {
        this.$router.push({
          path: '/groupProds',
          query: { activityId: groupActivityId }
        })
      }
      this.page = this.$refs.crud.$refs.tablePage.defaultPage
      this.getDataList(this.page)
    },
    searchChange (params) {
      this.searchForm = params
      this.page.currentPage = 1
      this.page.pageSize = 10
      this.getDataList(this.page)
    },
    // 管理拼团活动商品
    manageGroupProdHandle (groupActivityId) {
      this.visible = false
      this.$router.push({
        path: '/groupProds',
        query: { activityId: groupActivityId }
      })
    },
    // 失效活动活动
    invalidActivityHandle (row) {
      this.$confirm(this.$i18n.t('groups.determinedToFail') + row.activityName + this.$i18n.t('groups.actTip'), this.$i18n.t('text.tips'), {
        confirmButtonText: this.$i18n.t('crud.filter.submitBtn'),
        cancelButtonText: this.$i18n.t('crud.filter.cancelBtn'),
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/group/activity/invalid/' + row.groupActivityId),
          method: 'PUT',
          data: this.$http.adornData({})
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('publics.operation'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList(this.page)
            }
          })
        })
      }).catch(() => {
      })
    },
    // 启用拼团活动
    activeGroupActivity (groupActivityId) {
      this.$http({
        url: this.$http.adornUrl('/group/activity/active/' + groupActivityId),
        method: 'PUT',
        data: this.$http.adornData({})
      }).then(({ data }) => {
        this.$message({
          message: this.$i18n.t('publics.operation'),
          type: 'success',
          duration: 1500,
          onClose: () => {
            this.getDataList(this.page)
          }
        })
      })
    },
    // 下线管理
    auditEventHandle (id) {
      this.offlineEventHandleVisible = true
      this.$nextTick(() => {
        this.$refs.offlineEvent.init(id)
      })
    }
  }
}
</script>
<style lang="scss">
.mod-admin-groupActivity {
}
</style>
